//
//  ASIHttpTest.m
//  WJHttpEngineASI-example
//
//  Created by Yunhai.Wu on 15/11/6.
//  Copyright (c) 2015年 WJ. All rights reserved.
//

#import "ASIHttpTest.h"
#import "IQWHttpEngine.h"
#import "QWHttpFactory.h"

#import "TestRequest.h"
#import "TestResponse.h"
#import "QWHttpEngineConfig.h"
#import "ASIQWHttpEngine.h"

@interface ASIHttpTest ()
@property (nonatomic, strong) id<IQWHttpEngine> httpEngine;
@end

@implementation ASIHttpTest

-(void)setUp {
    [[QWHttpEngineConfig sharedInstance] setEngine:[ASIQWHttpEngine class]];
    
    if (!_httpEngine) {
        self.httpEngine = [QWHttpFactory buildHttpEngine];
    }
}

-(void) testSync {
    TestRequest *request = [[TestRequest alloc] init];
    NSError *error = nil;
    id<IQWHttpResponse> response = [_httpEngine syncRequest:request responseClass:[TestResponse class] error:&error];
    if ([response isError]) {
        GHTestLog(@"Sync request error :%@",error);
    } else {
        GHTestLog(@"Sync request success");
        GHTestLog(@"responseData:%@",[(TestResponse*)response responseData]);
    }
}

-(void) testAsync {
    TestRequest *request = [[TestRequest alloc] init];
    [_httpEngine asynRequest:request responseClass:[TestResponse class] responseBlock:^(id<IQWHttpResponse> res, NSError *error) {
        if (error) {
            GHTestLog(@"async request error:%@",error);
        } else {
            if ([res isError]) {
                GHTestLog(@"async request fail errorcode:%@",[res errorCode]);
            } else {
                NSLog(@"async request success");
                NSLog(@"responseData:%@",[(TestResponse*)res responseData]);
            }
        }
    }];
}


@end
